<?php
declare (strict_types = 1);

namespace app\admin\service\data\order;

use app\admin\model\data\ClassesOrder;
use app\admin\model\data\LiveOrder;
use app\admin\model\data\LiveSmallOrder;
use app\admin\model\data\StudentVipOrder;
use app\admin\model\data\VideoOrder;
use app\admin\service\data\DataService;

/**
 * 订单
 */
class Order extends DataService
{
    /**
     * 订单量
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-07-07
     * @return   [type]                         [description]
     */
    public function count()
    {
        $video      = $this->appendCommonConditions(app(VideoOrder::class))->count();
        $live       = $this->appendCommonConditions(app(LiveOrder::class), ['course' => ['course.course_live_type']])->where('course.course_live_type', 1)->count();
        $live_small = $this->appendCommonConditions(app(LiveSmallOrder::class), ['course' => ['course.course_live_type']])->where('course.course_live_type', 2)->count();
        $classes    = $this->appendCommonConditions(app(ClassesOrder::class))->count();
        $vip        = $this->appendCommonConditions(app(StudentVipOrder::class))->count();

        $rows = [];
        if ($video || $live || $live_small || $classes || $vip) {
            $rows = [
                ['course_type' => '点播课', 'order_count' => $video],
                ['course_type' => '大班课', 'order_count' => $live],
                ['course_type' => '小班课', 'order_count' => $live_small],
                ['course_type' => '班级课', 'order_count' => $classes],
                ['course_type' => '会员充值', 'order_count' => $vip],
            ];
        }

        $data = [
            'columns' => ['course_type', 'order_count'],
            'rows'    => $rows,
        ];

        return $this->parseToData($data, 1, '获取成功');
    }
    /**
     * 订单销售额
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-07-07
     * @param    string                         $value [description]
     * @return   [type]                                [description]
     */
    public function price($value = '')
    {
        $video      = $this->appendCommonConditions(app(VideoOrder::class))->sum('__TABLE__.payment');
        $live       = $this->appendCommonConditions(app(LiveOrder::class), ['course' => ['course.course_live_type']])->where('course.course_live_type', 1)->sum('__TABLE__.payment');
        $live_small = $this->appendCommonConditions(app(LiveSmallOrder::class), ['course' => ['course.course_live_type']])->where('course.course_live_type', 2)->sum('__TABLE__.payment');
        $classes    = $this->appendCommonConditions(app(ClassesOrder::class))->sum('__TABLE__.payment');
        $vip        = $this->appendCommonConditions(app(StudentVipOrder::class))->sum('__TABLE__.payment');
        $rows       = [];
        if ($video || $live || $live_small || $classes || $vip) {
            $rows = [
                ['course_type' => '点播课', 'order_price' => $video],
                ['course_type' => '大班课', 'order_price' => $live],
                ['course_type' => '小班课', 'order_price' => $live_small],
                ['course_type' => '班级课', 'order_price' => $classes],
                ['course_type' => '会员充值', 'order_price' => $vip],
            ];
        }

        $data = [
            'columns' => ['course_type', 'order_price'],
            'rows'    => $rows,
        ];

        return $this->parseToData($data, 1, '获取成功');
    }

    /**
     * 填充公共条件
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-07-08
     * @param    [type]                         $model [description]
     * @return   [type]                                [description]
     */
    private function appendCommonConditions($model, $withJoin = [])
    {
        $time = $this->parseTime();

        return $model->scope('mhm','__TABLE__.mhm_id')->withJoin(array_merge([
            'pdorder' => ['pdorder.status'],
        ], $withJoin))->whereIn('pdorder.status', [20, 30, 40])->whereBetween('pdorder.payment_time', $time);
    }
}
